<template>
  <div class="container">
    <el-button type="primary" size="small" class="add-button" @click="openAddDialog()">添加音乐</el-button>
    <!-- 列表 -->
    <el-table :data="musicList" border style="width: 100%">
      <el-table-column prop="id" label="编号" />
      <el-table-column prop="name" label="名称" />
      <el-table-column prop="artist" label="艺术家" />
      <el-table-column prop="url" label="链接" />

      <el-table-column prop="enable" label="启用">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.enable === 1">启用</el-tag>
          <el-tag v-else type="danger">未启用</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="lrc" label="歌词" />
      <!-- <el-table-column prop="cover" label="封面" /> -->
      <el-table-column prop="cover" label="封面">
        <template slot-scope="scope">
          <el-image
            style="width: 80%; height: 60px"
            :src="scope.row.cover"
            :preview-src-list="[scope.row.cover]"
          />
        </template>
      </el-table-column>

      <el-table-column label="操作" width="250">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" @click="handleEdit(scope.row.id)">编辑</el-button>
          <el-button v-if="scope.row.enable === 0" size="mini" type="success" @click="enableById(scope.row.id)">启用</el-button>
          <el-button v-else size="mini" type="warning" @click="disEnableById(scope.row.id)">弃用</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加弹框 -->
    <el-dialog
      title="添加音乐"
      :visible.sync="addDialog"
    >
      <!-- 向另一个页面传递 方法 -->
      <musicAdd @getmusicList="getmusicList" @closeAddDialog="closeAddDialog" />
    </el-dialog>
    <!-- 修改弹窗 -->
    <el-dialog
      title="修改音乐"
      :visible.sync="updateDialog"
    >
      <!-- 向另一个页面传递 方法 -->
      <musicUpdate :music="music" @closeUpdateDialog="closeUpdateDialog" @getmusicList="getmusicList" />
    </el-dialog>
  </div>
</template>
<script>
// 引入接口
import musicApi from '@/api/music'
import musicAdd from './music-add'
import musicUpdate from './music-update'
export default {
  components: {
    musicAdd,
    musicUpdate
  },
  data() {
    return {
      music: {}, // 音乐
      updateDialog: false, // 控制修改弹窗
      addDialog: false, // 控制添加弹窗打开关闭
      musicList: [] // 类型数组
    }
  },
  created() {
    this.getmusicList()
  },
  methods: {
    getmusicList() {
      // 查询音乐列表
      musicApi.findAll().then(response => {
        // console.log(response)
        this.musicList = response.data
      })
    },
    handleEdit(id) {
      // 修改音乐方法
      musicApi.findById(id).then(response => {
        this.music = response.data
        // 获取到数据
        // console.log(response.data)
        this.updateDialog = true
      })
    },
    handleDelete(id) {
      // 删除音乐方法
      this.$confirm('此操作将永久删除！, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        music: 'error'
      })
        .then(() => {
          musicApi.deleteById(id).then(response => {
            this.$message.success(response.msg)
            // 删除成功 刷新列表
            this.getmusicList()
          })
        })
        .catch(() => {
          this.$message({
            music: 'info',
            message: '已取消删除'
          })
        })
    },
    openAddDialog() {
      // 打开弹窗
      this.addDialog = true
    },
    closeAddDialog() {
      // 关闭添加音乐窗口
      this.addDialog = false
    },
    closeUpdateDialog() {
      // 关闭修改弹窗
      this.updateDialog = false
    },
    enableById(id) {
      // 启用 音乐
      musicApi.enableById(id).then(response => {
        this.$message.success(response.msg)
        // 启用成功 刷新列表
        this.getmusicList()
      })
    },
    disEnableById(id) {
      // 弃用音乐
      musicApi.disEnableById(id).then(response => {
        this.$message.success(response.msg)
        // 弃用成功 刷新列表
        this.getmusicList()
      })
    }
  }
}
</script>

